package cst.a722wr.zyfypt_722wr.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a722wr.zyfypt_722wr.Activity.ProjectActivity;
import cst.a722wr.zyfypt_722wr.Bean.ProjectBean;
import cst.a722wr.zyfypt_722wr.R;

public class ProjectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private List<ProjectBean> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public ProjectAdapter(Context context, List<ProjectBean> list)
    {
        this.inflater = LayoutInflater.from(context);
        this.list=list;
        this.context=context;
    }

    public ProjectAdapter(Context context)
    {
        this.inflater = LayoutInflater.from(context);
        this.context=context;
    }


    public void setList(List<ProjectBean> l){
        list=l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.item,parent,false);
        RecyclerView.ViewHolder vh = new ProjectAdapter.ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ProjectBean bean = list.get(position);
        if(bean==null) return;
        final ProjectAdapter.ViewHolder viewHolder= (ProjectAdapter.ViewHolder) holder;
        viewHolder.thumb.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.tware_default));
        viewHolder.name.setText(bean.getName());
        viewHolder.insert_time.setText(bean.getUpdate_time());
        viewHolder.description.setText(getLimitLengthString(bean.getDescription(),58,"..."));
        //viewHolder.itemView.setTag(bean);
        //if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    Intent intent = new Intent(context, ProjectActivity.class);
                    intent.putExtra("resid", list.get(position).getId());
                    intent.putExtra("userid", list.get(position).getUserid());
                    context.startActivity(intent);
                    //mOnItemClickListener.onItemClick(viewHolder.itemView, position);
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    //mOnItemClickListener.onItemLongClick(viewHolder.itemView, position);
                    return false;
                }
            });
        //}
        //使用同样开发团队的Picasso支持包进行图片加载，由于接口中返回的img路径不是全的，所以需要加上网站前缀
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+bean.getThumb())
                .placeholder(R.drawable.tware_default)
                .into(viewHolder.thumb);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }






    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView insert_time;
        TextView description;
        ImageView thumb;

        public ViewHolder(View itemView) {
            super(itemView);
            thumb = (ImageView) itemView
                    .findViewById(R.id.item_thumb);
            name = (TextView) itemView
                    .findViewById(R.id.item_name);
            insert_time = (TextView) itemView
                    .findViewById(R.id.item_insert_time);
            description = (TextView) itemView
                    .findViewById(R.id.item_description);

        }
    }
    private String getLimitLengthString(String str,int len, String symbol){
        if(str.length()<len)
            return str;
        else{
            return str.substring(0,len-1)+symbol;
        }
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
